using System;
using System.Linq;
using NUnit.Framework;
using System.Threading;

namespace QuickBencher.Test
{
    public class SomeComponent
    {
        public void DoSomeWork()
        {
            Thread.Sleep(200);
        }
        public void DoSomeWork3()
        {
            Thread.Sleep(400);
        }
        public void DoSomeWork2()
        {
            Thread.Sleep(300);
        }
    }

    [TestFixture]
    public class Showing_off_benchmark_usage_in_tests
    {
        [Test]
        public void Some_test()
        {
            // Given
            var someComponent = new SomeComponent();
            // When
            var measurement = Benchmark.Measure(() => someComponent.DoSomeWork());
            // Then
            Assert.Less(measurement.RealTime, 0.98d,
                "SLA failed: 'Some Work' should take less than 0.98 seconds.");
        }

        [Test]
        public void Some_other_test()
        {
            // Given
            var someComponent = new SomeComponent();
            // When
            var measurements = Benchmark.Bmbm((x) =>
            {
                x.Report("Method 1", () => someComponent.DoSomeWork());
                x.Report("Method 2", () => someComponent.DoSomeWork2());
                x.Report("Method 3", () => someComponent.DoSomeWork3());
            });
            // Then 
            var result1 = measurements.Single((m) => m.Label.Equals("Method 1"));
            var result2 = measurements.Single((m) => m.Label.Equals("Method 2"));
            var result3 = measurements.Single((m) => m.Label.Equals("Method 3"));
            Assert.Less(result1.RealTime, result2.RealTime, "Method 1 should be faster than 2");
            Assert.Less(result2.RealTime, result3.RealTime, "Method 2 should be faster than 3");
        }
    }
}
